<?php
// src/Acme/StoreBundle/Model/LinkLogic.php
namespace Asiah\SiklusBundle\Model;

use Asiah\SiklusBundle\Entity\Link;

class Linklogic {

	public function generateLink($lastlink,$index) {
		//$index = $lastlink[6];
		if($index >= 0) {
			//$lastlink = substr($lastlink, 0, 6);
			$tmp = $lastlink[$index];
			if($lastlink[$index] == '9') {
				$lastlink[$index] = 'a';
			} else if($lastlink[$index] == 'z') {
				$lastlink[$index] = 'A';
			} else if($lastlink[$index] == 'Z') {
				$lastlink[$index] = '0';
				//$retlink = $this -> generateLink($lastlink , $this -> decrementIndex($index));
				//$lastlink = $retlink[0];
			} else {
				$tmp++;
				$lastlink[$index] = $tmp;
			}
			//return $lastlink . $this -> decrementIndex($index);
			return array($lastlink, $this-> decrementIndex($index));
		}
		return array($lastlink, $this-> decrementIndex($index));
		//return $lastlink . $this -> decrementIndex($index);
	}

	public function decrementIndex($index) {
		$index = $index - 1;
		if($index < 0)
			$index = 5;
		return $index;
	}

	public function validateURL($url)
	{
		$pattern = '/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i';
		return preg_match($pattern, $url);
	}
	
	public function getMetaData($url){
		$content = file_get_contents($url);
		$pattern = "|<[\s]*title[\s]*>([^<]+)<[\s]*/[\s]*title[\s]*>|Ui";
		$returnvalue;
		if(preg_match($pattern, $content, $match))
			$returnvalue['title'] = $match[1];
		else
			return null;
		$tags = get_meta_tags($url);
		$returnvalue['description'] = isset($tags['description'])? $tags['description'] : '';  	
		//echo 'hoh '.$tags['keywords'];
		//$returnvalue['tags'] = $isset($tags['keywords'])? $tags['keywords'] : array('');  	
		//$returnvalue['tags']= $tags['keywords'];
          return $returnvalue;
	}
}
